home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Examples / gwcopy / GWCopyApp.m < prev    next >
Encoding:
Text File  |  1992-02-03  |  2.9 KB  |  124 lines

  1. //#import <stdio.h>
  2.  
  3. #import <stdio.h>
  4. #import <stdlib.h>
  5. #import <strings.h>
  6. #import <sys/param.h>
  7. #import <streams/streams.h>
  8. #import <appkit/NXBitmapImageRep.h>
  9. #import <appkit/Pasteboard.h>
  10. #import <appkit/Listener.h>
  11. #import <appkit/Speaker.h>
  12. #import "GWCopyApp.h"
  13. #import "ControlLoader.h"
  14. #import "NXBitmapImageRepControl.h"
  15. #import "Converter.h"
  16.  
  17. @implementation GWCopyApp
  18.  
  19. char *mktemp(char *name);
  20.  
  21. - run: (int)argc : (char **)argv
  22. {
  23.     id            bitmaps = [[[ControlLoader loadControl: "Bitmap"] alloc] init];
  24.     id            image;
  25.     id             converter;
  26.     char            *type = NULL;
  27.     char            filename[MAXPATHLEN];
  28.     char            *application = "GraphicsWorkshop";
  29.     int            x;
  30.     BOOL        messageGW = NO;
  31.     NXStream    *myStream;
  32.     
  33.     if (argc == 1) {
  34.         fprintf(stderr, "You must specify at least the file type\n");
  35.         exit(1);
  36.     } else {
  37.         for (x = 1; x < argc; x++) {
  38.             if (argv[x][0] == '-') {
  39.                 if (strlen(argv[x]) == 1) {
  40.                     strcpy(filename, "-");
  41.                 } else {
  42.                     switch (argv[x][1]) {
  43.                         case 'a':
  44.                         case 'A':
  45.                             application = argv[++x];
  46.                             break;
  47.                         case 'p':
  48.                         case 'P':
  49.                             messageGW = YES;
  50.                             break;
  51.                         case 't':
  52.                         case 'T':
  53.                             type = argv[++x];
  54.                             break;
  55.                         default:
  56.                             fprintf(stderr, "Unknown argument %s\n", argv[x]);
  57.                             exit (1);
  58.                     }
  59.                 }
  60.             } else {
  61.                 strcpy(filename, argv[x]);
  62.             }
  63.         }
  64.         
  65.         if (!strcmp(filename, "-")) {
  66.             if (type == NULL) {
  67.                 type = "tiff";
  68.             }
  69.             [bitmaps handleLink: type];
  70.             converter = [bitmaps getCurrentConverter];
  71.             fprintf(stderr, "Using Format: %s\n", [converter getFormatName]);
  72.             myStream = NXOpenFile(fileno(stdin), NX_READONLY);
  73.             image = [converter readFromStream: myStream from: bitmaps];
  74.             if ([converter errorState]) {
  75.                 fprintf(stderr, "Converter Error: %d\n", [converter errorMessage]);
  76.                 exit(1);
  77.             }
  78.         } else {
  79.             image = [bitmaps openAndReturnImage: filename];
  80.         }
  81.         [self copyToPBoard: image];
  82.         [image free];
  83.         
  84.         if (messageGW) {
  85.             int        msgDelivered, pasted;
  86.             id        mySpeaker = [[Speaker alloc] init]; 
  87.             port_t     thePort = NXPortFromName(application, NULL);
  88.             
  89.             if (thePort != PORT_NULL) {
  90.                 [mySpeaker setSendPort: thePort];
  91.                 msgDelivered = [mySpeaker msgPaste: &pasted];
  92.                 if (msgDelivered != 0) {
  93.                     fprintf(stderr, "Unable to deliver paste message, Mach Error = %d\n",
  94.                                          msgDelivered);
  95.                 }
  96.             }
  97.             [mySpeaker free];
  98.         }
  99.     }
  100.     
  101.     [bitmaps free];
  102.  
  103.     return self;
  104. }
  105.  
  106. - copyToPBoard: (id)thePic
  107. {
  108.         id            myPboard = [Pasteboard newName: NXSelectionPboard];
  109.         NXStream    *myStream;
  110.         char            *data;
  111.         int            length, maxLength;
  112.  
  113.         [myPboard declareTypes: &NXTIFFPboardType num:1 owner:self];
  114.         myStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  115.         [thePic writeTIFF: myStream];
  116.         NXGetMemoryBuffer(myStream, &data, &length, &maxLength);
  117.         [myPboard writeType: NXTIFFPboardType data: data length: length];
  118.         NXCloseMemory(myStream, NX_FREEBUFFER);
  119.  
  120.         return self;
  121. }
  122.  
  123. @end
  124.